#include "tooldb.h"
#include "dbmanager.h"
#include "networktool.h"

ToolDB::ToolDB()
{
}

QString ToolDB::openDB(const QString &fileName)
{
    QString json = "";
    QString uuid = "";
    QString msg = "";
    if(dbManager::getInstance().openDB(fileName,uuid,msg))
    {
        json = JsonTool::success(uuid);
    }else{
        json = JsonTool::failed(-1,msg);
    }
    return json;
}

QString ToolDB::selectDataMultiple(const QString &sql)
{
    QString json = "";
    QString uuid = "";
    QString msg = "";
    QJsonArray array;

    if(dbManager::getInstance().selectDataMultiple(sql,uuid,msg,array))
    {
        json = JsonTool::success(array);
    }else{
        json = JsonTool::failed(-2,msg);
    }
    return json;
}

QString ToolDB::selectDataSingle(const QString &sql)
{
    QString json = "";
    QString uuid = "";
    QString msg = "";
    QJsonObject jobject;

    if(dbManager::getInstance().selectDataSingle(sql,uuid,msg,jobject))
    {
        json = JsonTool::success(jobject);
    }else{
        json = JsonTool::failed(-2,msg);
    }
    return json;
}

QString ToolDB::addData(const QString &sql)
{
    QString json = "";
    QString uuid = "";
    QString msg = "";

    if(dbManager::getInstance().addData(sql,uuid,msg))
    {
        json = JsonTool::success("");
    }else{
        json = JsonTool::failed(-2,msg);
    }
    return json;
}

QString ToolDB::deleteData(const QString &sql)
{
    QString json = "";
    QString uuid = "";
    QString msg = "";

    if(dbManager::getInstance().deleteData(sql,uuid,msg))
    {
        json = JsonTool::success("");
    }else{
        json = JsonTool::failed(-2,msg);
    }
    return json;
}

QString ToolDB::updateData(const QString &sql)
{
    QString json = "";
    QString uuid = "";
    QString msg = "";

    if(dbManager::getInstance().deleteData(sql,uuid,msg))
    {
        json = JsonTool::success("");
    }else{
        json = JsonTool::failed(-2,msg);
    }
    return json;
}

QString ToolDB::sendGet(const QString &url, const QString &headerJson, int timeout)
{
    QString json = "";
    QString rtn = "";
    QString msg = "";

    if(g_net.sendGet(url,headerJson,timeout,rtn,msg))
    {
        json = JsonTool::success(rtn);
    }else{
        json = JsonTool::failed(-3,msg);
    }
    return json;
}

QString ToolDB::sendPost(const QString &url, const QString &headerJson, QString paramJson,int timeout)
{
    QString json = "";

    QString rtn = "";
    QString msg = "";

    if(g_net.sendPost(url,headerJson,paramJson,timeout,rtn,msg))
    {
        json = JsonTool::success(rtn);
    }else{
        json = JsonTool::failed(-3,msg);
    }
    return json;
}
